﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using UploadClient.Utilitys;

namespace UploadClient
{
    public class UpdateManager
    {
        #region Delegates

        public delegate void UpdateCheckingHanlder(bool bVal);

        #endregion

        public event UpdateCheckingHanlder OnUpdateCheck;
              


        public void CleanUp()
        {
            try
            {
                if (File.Exists("Updates.xml"))
                    File.Delete("Updates.xml");
            }
            catch (Exception ex)
            {
                //Do nothing 
                //Since we are providing a silent update feature as of now
                //The catch block is not handled here
            }


        }

        public void StartUpdater()
        {
            Thread thUpdate = new Thread(new ThreadStart(StartUpdatingProcess));
            thUpdate.IsBackground = true;
            thUpdate.Start();


        }

        public void UpdateChecker()
        {
            Thread chkUpdate = new Thread(new ThreadStart(StartUpdateChecker));
            chkUpdate.IsBackground = true;
            chkUpdate.Start();
        }

        private void StartUpdateChecker()
        {
            LatestVersionChecker latestVersionCheck = new LatestVersionChecker();

            bool bIsLatestVersionExists = latestVersionCheck.CheckLatestVersionPresent();

            OnUpdateCheck(bIsLatestVersionExists);

            if (!bIsLatestVersionExists)
                CleanUp();

        }
        /// <summary>
        /// This method is to be used if updates.xml has to be got and upgrading process needs to be started after that.
        /// This method has been written to handle the scenario, 
        /// If user deletes the updates.xml, and the user is trying to upgrade after that. 
        /// </summary>
        public void ChkAndUpgrade()
        {
            Thread chkAndUpgradeThread = new Thread(new ThreadStart(CheckAndUpgrade));
            chkAndUpgradeThread.IsBackground = true;
            chkAndUpgradeThread.Start();

        }

        private void CheckAndUpgrade()
        {
            LatestVersionChecker latestVersionCheck = new LatestVersionChecker();

            bool bIsLatestVersionExists = latestVersionCheck.CheckLatestVersionPresent();


            if (!bIsLatestVersionExists)
                CleanUp();
            else
            {

                StartUpdatingProcess();

            }

        }
        private void StartUpdatingProcess()
        {

            try
            {
                
                bool bOtherFtpUploadClientProcessExists = false;

                //if there are any other FTPUploadClient processes
                Process[] appProcesses = Process.GetProcessesByName("FtpUploadClient");
                Process currProcess = Process.GetCurrentProcess();
                foreach (Process proc in appProcesses)
                {
                    if (proc.Id != currProcess.Id)
                    {
                        bOtherFtpUploadClientProcessExists = true;
                    }
                }

                if (bOtherFtpUploadClientProcessExists)
                {
                    //Inform the user that upde cannot be called
                    //And Close all the other instances of Updater
                }
                else
                {
                    //Kill the current 
                    Process process = new Process();

                    process.StartInfo.FileName = "UpdaterAppWPF.exe";

                    process.Start();
                    currProcess.Kill();
                }

            }
            catch (Exception ex)
            {
                if (File.Exists("Updater.xml"))
                    File.Delete("Updater.xml");
            }

        }

        

       






    }
}
